package 二叉树相关问题;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class 第二小的数 {
    List<Integer> list = new ArrayList<>();
    public int findSecondMinimumValue(TreeNode root) {
        if (root==null || (root.left==null && root.right==null ))
            return -1;
        traverse(root);
        Collections.sort(list);
        int res=list.get(0);
        for (int i = 1; i < list.size(); i++) {
            if (list.get(i)>res){
                res=list.get(i);
                break;
            }
        }
        return res==list.get(0)?-1:res;
    }

    private void traverse(TreeNode root) {
        if (root==null)
            return;
        traverse(root.left);
        list.add(root.val);
        traverse(root.right);
    }
}
